package com.daxia.bumall.service;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/** 
* 类名称：BaseExcelCallback 
* 类描述：excel导出抽象类 
* @version 
*/
public abstract class BaseExcelCallback<T> implements IExcelCallback<T> {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /** 
     * 生成表头样式
    * @param wb
    * @return
    */
    protected CellStyle buildHeaderStyle(Workbook wb) {
        //设置字体 加粗
        Font font = wb.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        //设置表头样式
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        return style;
    }

    /** 
     * 带样式创建单元格
    * @param row
    * @param column
    * @param val
    * @param style
    * @return
    */
    protected Cell createCell(Row row, int column, Object val, CellStyle style) {
        Object obj = null == val ? "" : val;
        Cell cell = null;
        if (obj instanceof Number) {
            cell = row.createCell(column, Cell.CELL_TYPE_NUMERIC);
            Number n = ((Number) obj);
            cell.setCellValue(n.doubleValue());
        } else {
            cell = CellUtil.createCell(row, column, obj.toString());
        }
        if (null != style) {
            cell.setCellStyle(style);
        }
        return cell;
    }

    /** 
     * 创建单元格
     * @param row
     * @param column
     * @param val
     * @return
     */
    protected Cell createCell(Row row, int column, Object val) {
        return createCell(row, column, val, null);
    }

    @Override
    public void layout(Sheet sheet) {
        //do nothing
    }
}
